package com.myproject.security.simpledemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "欢迎访问,simple-demo";
    }

    @GetMapping("/user")
    public Authentication getUser(Authentication authentication) {
        log.info("resource: user {}", authentication);
        return authentication;
    }

    @GetMapping("/admin/hello")
    public String adminV1() {
        return "hello admin";
    }

    @GetMapping("/user/hello")
    public String userV1() {
        return "hello user";
    }

    @Secured("ROLE_ADMIN")
    @GetMapping("/admin/hello/v2")
    public String adminV2() {
        return "hello admin";
    }

    @PreAuthorize("hasAnyRole('ADMIN','DBA','USER')")
    @GetMapping("/user/hello/v2")
    public String userV2() {
        return "hello user";
    }

    @GetMapping("/db/hello")
    public String db() {
        return "hello db";
    }

}